using AntdUI;
using System;
using System.Windows.Forms;
using System.Xml.Linq;
using TangLiveRecorder.Controls;
using TangLiveRecorder.Services;
using static TangLiveRecorder.Data;

namespace TangLiveRecorder
{
    public partial class MainWindow : AntdUI.Window, ITransient
    {
        public MainWindow()
        {
            InitializeComponent();

            LoadMenu();
            BindEventHandler();
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            // 记录应用程序启动日志
            AddLog("应用程序启动");
            AddLog($"系统时间: {DateTime.Now}");
            AddLog($"系统版本: {Environment.OSVersion}\r\n");

            LoadControls();
            menuNavigation.SelectIndex(0);
        }

        private void LoadControls()
        {
            UserControl live = App.GetRequiredService<LiveControl>();
            panelContent.Controls.Add(live);

            UserControl file = App.GetRequiredService<FileControl>();
            panelContent.Controls.Add(file);

            UserControl log = App.GetRequiredService<LogControl>();
            panelContent.Controls.Add(log);

            UserControl setting = App.GetRequiredService<SettingControl>();
            panelContent.Controls.Add(setting);
        }

        private void LoadMenu()
        {
            menuNavigation.Items.Clear();
            foreach (MenuItem item in Data.menuItems)
            {
                menuNavigation.Items.Add(item);
            }
        }

        private void BindEventHandler()
        {
            btnMenuToggle.Click += BtnMenuToggle_Click;
            menuNavigation.SelectChanged += MenuNavigation_SelectChanged;
            this.FormClosing += MainWindow_FormClosing;
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                AddLog("应用程序正在关闭，停止所有服务...");

                // 停止轮询服务
                var pollingService = App.GetRequiredService<PollingService>();
                pollingService?.Stop();
                AddLog("轮询服务已停止");

                // 停止所有录制任务
                var recordingService = App.GetRequiredService<RecordingService>();
                recordingService?.StopAllRecordingsAsync().Wait(5000); // 等待最多5秒
                AddLog("所有录制任务已停止");

                // 释放资源
                pollingService?.Dispose();
                recordingService?.DisposeAsync().Wait(3000); // 等待最多3秒

                AddLog("应用程序关闭完成");
            }
            catch (Exception ex)
            {
                AddLog($"关闭应用程序时发生错误: {ex.Message}");
            }
        }

        private void MenuNavigation_SelectChanged(object sender, MenuSelectEventArgs e)
        {
            string name = (string)e.Value.Tag;
            var panelControls = panelContent.Controls;
            bool isHasControl = false;
            if (panelControls.Count > 0)
            {
                foreach (Control item in panelControls)
                {
                    if (item.Tag.ToString() != name)
                    {
                        item.Visible = false;
                    }
                    else
                    {
                        item.Visible = true;
                        isHasControl = true;
                    }
                }
            }
            if (isHasControl) return;
        }

        private void BtnMenuToggle_Click(object? sender, EventArgs e)
        {
            if (menuNavigation.Collapsed)
            {
                menuNavigation.Width = (int)(200 * Config.Dpi);
            }
            else
            {
                menuNavigation.Width = (int)(70 * Config.Dpi);
            }
            btnMenuToggle.Toggle = !btnMenuToggle.Toggle;
            menuNavigation.Collapsed = !menuNavigation.Collapsed;
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
        }
    }
}